using System;
using System.Reflection;

namespace UPA
{
    public struct ProcessActionResult
    {
        public bool Finished { get; private set; }
        public string ErrorMessage { get; private set; }

        public ProcessActionResult(bool finished)
        {
            Finished = finished;
            ErrorMessage = string.Empty;
        }
        
        public ProcessActionResult(bool finished, string errorMessage)
        {
            Finished = finished;
            ErrorMessage = errorMessage;
        }
    }
    public abstract class ProcessAction
    {
        public ProcessActionArgument Argument { get; private set; }
        public virtual Type RequiredArgument => null;
        public virtual string Name => GetTemplateName();

        protected T GetArgument<T>() where T: ProcessActionArgument
        {
            return Argument as T;
        }

        public void SetArgument(ProcessActionArgument argument)
        {
            Argument = argument;
            OnArgumentLoaded();
        }
        
        protected virtual void OnArgumentLoaded()
        {
            
        }

        protected string GetTemplateName()
        {
            var type = GetType();
            return type.GetCustomAttribute<ProcessActionTemplateNameAttribute>()?.Name ?? type.Name;
        }

        public abstract ProcessActionResult Execute();
    }
}